package ms.unicorn.util;

import org.eclipse.gmf.runtime.emf.type.core.IElementType;

import ms.unicorn.Diagram;
import ms.unicorn.Element;
import ms.unicorn.modellanguage.HintedSpecializationType;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageRegistry;

public class LanguageUtil {
	
	public static boolean isCompability(Element element, String typeId) {
		return element != null && element.getElementType().equalsIgnoreCase(typeId);
	}
	
	public static Element findContainer(Element candidate, IElementType targetType) {
		return findContainer(candidate, resolveLanguageElement(targetType));
	}
	
	public static Element findContainer(Element candidate, LanguageElement element) {		
		while(true) {
			if (candidate instanceof Diagram)
				return candidate;
			LanguageElement le = LanguageRegistry.INSTANCE.getLanguageElement(candidate);
			if (le.acceptChild(element))
				return candidate;
			candidate = (Element) candidate.eContainer();
		}
	}
	
	public static LanguageElement resolveLanguageElement(IElementType type) {
		if (type instanceof HintedSpecializationType)
			return ((HintedSpecializationType) type).getElement();
		return null;
	}
}
